<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: bm_comfort.php 503 2014-01-31 16:27:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 503 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-31 16:27:11 +0000 (Fri, 31 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class bm_comfort {
	var $code, $title, $description, $icon, $enabled, $db;

	function __construct() {
		global $order, $db;

		$this->code = 'bm_comfort';
		$this->title = MODULE_SHIPPING_BM_COMFORT_TEXT_TITLE;
		$this->description = MODULE_SHIPPING_BM_COMFORT_TEXT_DESCRIPTION;
		$this->sort_order = MODULE_SHIPPING_BM_COMFORT_SORT_ORDER;
		$this->icon = '';
		$this->tax_class = MODULE_SHIPPING_BM_COMFORT_TAX_CLASS;
		$this->enabled = ((MODULE_SHIPPING_BM_COMFORT_STATUS == 'True') ? true : false);
		$this->db = $db;

		if (($this->enabled == true) && ((int)MODULE_SHIPPING_BM_COMFORT_ZONE > 0) && isset($order)) {
			$check_flag = false;

			$check = $this->db->db_query("SELECT zone_id FROM ".TABLE_ZONES_TO_GEO_ZONES." WHERE geo_zone_id = '".MODULE_SHIPPING_BM_COMFORT_ZONE."' AND zone_country_id = '".$order->delivery['country']['id']."' ORDER BY zone_id");

			while(!$check->EOF) {
				if($check->fields['zone_id'] < 1) {
					$check_flag = true;
					break;
				} elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
					$check_flag = true;
					break;
				}
				$check->MoveNext();
			}

			if($check_flag == false)
				$this->enabled = false;
		}
	}

	function get_shipping_costs() {
		$order_total_modules = new order_total();
		$order_total_modules->values_from_cart = true;	
		$order_total_array = $order_total_modules->process(array('ot_subtotal'));
		$order_netto = $order_total_array['ot_subtotal']['netto'];
		$shipping_cost = MODULE_SHIPPING_BM_COMFORT_COST;
		
		if ($order_netto * (MODULE_SHIPPING_BM_COMFORT_COST_PERCENT / 100.0) > $shipping_cost)
			$shipping_cost = $order_netto * (MODULE_SHIPPING_BM_COMFORT_COST_PERCENT / 100.0);
		
		$shipping_cost += MODULE_SHIPPING_BM_STANDARD_COST;
		return $shipping_cost;
	}


	function quote($method = '') {
		if(!free_shipping()) return;
		global $order;
		$this->quotes = array(
			'id' => $this->code,
			'module' => MODULE_SHIPPING_BM_COMFORT_TEXT_TITLE,
			'methods' => array(
				array(
					'id' => $this->code,
					'module' => MODULE_SHIPPING_BM_COMFORT_TEXT_TITLE,
					'title' => MODULE_SHIPPING_BM_COMFORT_TEXT_WAY,
					'description' => MODULE_SHIPPING_BM_COMFORT_TEXT_DESCRIPTION,
					'cost' => $this->get_shipping_costs()
				)
			)
		);

		if ($this->tax_class > 0)
			$this->quotes['tax'] = get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);

		if (not_null($this->icon))
			$this->quotes['icon'] = image($this->icon, $this->title);

		return $this->quotes;
	}

	function check() {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_SHIPPING_BM_COMFORT_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
	}

    function install() {
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_STATUS', 'True', '6', '0', 'cfg_select_option(array(\'True\', \'False\'), ', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_ALLOWED', '', '6', '0', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_COST', '5.00', '6', '0', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_COST_PERCENT', '5.00', '6', '0', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_TAX_CLASS', '0', '6', '0', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_ZONE', '0', '6', '0', 'get_zone_class_title', 'cfg_pull_down_zone_classes(', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_BM_COMFORT_SORT_ORDER', '0', '6', '0', now())");
    }

    function remove() {
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key in ('".implode("', '", $this->keys())."')");
    }

    function keys() {
		return array('MODULE_SHIPPING_BM_COMFORT_STATUS',
					'MODULE_SHIPPING_BM_COMFORT_COST',
					'MODULE_SHIPPING_BM_COMFORT_COST_PERCENT',
					'MODULE_SHIPPING_BM_COMFORT_ALLOWED',
					'MODULE_SHIPPING_BM_COMFORT_TAX_CLASS',
					'MODULE_SHIPPING_BM_COMFORT_ZONE',
					'MODULE_SHIPPING_BM_COMFORT_SORT_ORDER');
    }
}
?>